import apiRequest from '@/utils/apiRequest';

/**
 * 获取客户列表
 * @param {Object} params - 查询参数
 */
export const getCustomers = async (params) => {
  try {
    const response = await apiRequest.get('/Customers/search', { params });
    return response.data;
  } catch (error) {
    console.error('Failed to get customers:', error);
    throw error;
  }
};

/**
 * 获取客户列表（别名，兼容旧调用）
 * @param {Object} params - 查询参数
 */
export const getCustomerList = getCustomers;

/**
 * 获取客户详情
 * @param {string} customerCode - 客户编码
 */
export const getCustomer = async (customerCode) => {
  try {
    const response = await apiRequest.get(`/Customers/${customerCode}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to get customer ${customerCode}:`, error);
    throw error;
  }
};

/**
 * 创建客户
 * @param {Object} customerData - 客户数据
 */
export const createCustomer = async (customerData) => {
  try {
    const response = await apiRequest.post('/Customers', customerData);
    return response.data;
  } catch (error) {
    console.error('Failed to create customer:', error);
    throw error;
  }
};

/**
 * 更新客户信息
 * @param {string} customerCode - 客户编码
 * @param {Object} customerData - 客户数据
 */
export const updateCustomer = async (customerCode, customerData) => {
  try {
    const response = await apiRequest.put(`/Customers/${customerCode}`, customerData);
    return response.data;
  } catch (error) {
    console.error(`Failed to update customer ${customerCode}:`, error);
    throw error;
  }
};

/**
 * 删除客户
 * @param {string} customerCode - 客户编码
 */
export const deleteCustomer = async (customerCode) => {
  try {
    const response = await apiRequest.delete(`/Customers/${customerCode}`);
    return response.data;
  } catch (error) {
    console.error(`Failed to delete customer ${customerCode}:`, error);
    throw error;
  }
};

/**
 * 更新客户状态
 * @param {string} customerCode - 客户编码
 * @param {string} status - 客户状态 ('启用' 或 '禁用')
 */
export const updateCustomerStatus = async (customerCode, status) => {
  try {
    const response = await apiRequest.patch(`/Customers/${customerCode}/status`, {
      status
    });
    return response.data;
  } catch (error) {
    console.error(`Failed to update status for customer ${customerCode}:`, error);
    throw error;
  }
};